package com.atguigu.daijia.coupon.controller;

import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.coupon.service.CouponInfoService;
import com.atguigu.daijia.model.entity.coupon.CouponInfo;
import com.atguigu.daijia.model.form.coupon.UseCouponForm;
import com.atguigu.daijia.model.vo.base.PageVo;
import com.atguigu.daijia.model.vo.coupon.AvailableCouponVo;
import com.atguigu.daijia.model.vo.coupon.NoReceiveCouponVo;
import com.atguigu.daijia.model.vo.coupon.NoUseCouponVo;
import com.atguigu.daijia.model.vo.coupon.UsedCouponVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;


@Tag(name = "优惠券活动接口管理")
@RestController
@RequestMapping(value="/coupon/info")
@SuppressWarnings({"unchecked", "rawtypes"})
@RequiredArgsConstructor
public class CouponInfoController {

    private final CouponInfoService couponInfoService;

    /**
     * 分页查询乘客未领取的优惠券列表
     * @param customerId
     * @param page
     * @param limit
     * @return
     */
    @Operation(summary = "分页查询乘客未领取的优惠券列表")
    @GetMapping("/findNoReceivePage/{customerId}/{page}/{limit}")
    public Result<PageVo<NoReceiveCouponVo>> findNoReceiveCouponPage(@PathVariable("customerId") Long customerId, @PathVariable("page") Long page, @PathVariable("limit") Long limit) {
        IPage<CouponInfo> pageParam = new Page<>(page, limit);
        PageVo<NoReceiveCouponVo> pageVo = couponInfoService.findNoReceiveCouponPage(pageParam, customerId);
        pageVo.setPage(page);
        pageVo.setLimit(limit);
        return Result.ok(pageVo);
    }

    /**
     * 分页查询乘客未使用的优惠券列表
     * @param customerId
     * @param page
     * @param limit
     * @return
     */
    @Operation(summary = "分页查询乘客未使用的优惠券列表")
    @GetMapping("/findNoUsePage/{customerId}/{page}/{limit}")
    public Result<PageVo<NoUseCouponVo>> findNoUseCouponPage(@PathVariable("customerId") Long customerId, @PathVariable("page") Long page, @PathVariable("limit") Long limit) {
        IPage<CouponInfo> pageParam = new Page<>(page, limit);
        PageVo<NoUseCouponVo> pageVo = couponInfoService.findNoUseCouponPage(pageParam, customerId);
        pageVo.setPage(page);
        pageVo.setLimit(limit);
        return Result.ok(pageVo);
    }

    /**
     * 分页查询乘客已使用的优惠券列表
     * @param customerId
     * @param page
     * @param limit
     * @return
     */
    @Operation(summary = "分页查询乘客已使用的优惠券列表")
    @GetMapping("/findUsedPage/{customerId}/{page}/{limit}")
    public Result<PageVo<UsedCouponVo>> findUsedCouponPage(@PathVariable("customerId") Long customerId, @PathVariable("page") Long page, @PathVariable("limit") Long limit) {
        IPage<CouponInfo> pageParam = new Page<>(page, limit);
        PageVo<UsedCouponVo> pageVo = couponInfoService.findUsedCouponPage(pageParam, customerId);
        pageVo.setPage(page);
        pageVo.setLimit(limit);
        return Result.ok(pageVo);
    }

    /**
     * 领取优惠券
     * @param customerId
     * @param couponId
     * @return
     */
    @Operation(summary = "领取优惠券")
    @GetMapping("/receive/{customerId}/{couponId}")
    public Result<Boolean> receiveCoupon(@PathVariable("customerId") Long customerId, @PathVariable("couponId") Long couponId) {
        Boolean result = couponInfoService.receiveCoupon(customerId, couponId);
        return Result.ok(result);
    }

    /**
     * 查询可用优惠券列表并按照优惠力度降序排列
     * @param customerId
     * @param orderAmount
     * @return
     */
    @Operation(summary = "查询可用优惠券列表并按照优惠力度降序排列")
    @GetMapping("/findAvailableCoupon/{customerId}/{orderAmount}")
    public Result<List<AvailableCouponVo>> findAvailableCouponList(@PathVariable("customerId") Long customerId, @PathVariable("orderAmount") BigDecimal orderAmount) {
    	List<AvailableCouponVo> list = couponInfoService.findAvailableCouponList(customerId, orderAmount);
    	return Result.ok(list);
    }

    /**
     * 使用优惠券
     * @param useCouponForm
     * @return
     */
    @Operation(summary = "使用优惠券")
    @PostMapping("/useCoupon")
    public Result<BigDecimal> useCoupon(@RequestBody UseCouponForm useCouponForm) {
        BigDecimal result = couponInfoService.useCoupon(useCouponForm);
        return Result.ok(result);
    }
}

